package com.snow.campuslink.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.snow.campuslink.annotation.RequestLog;
import com.snow.campuslink.project.BaseRestController;
import com.snow.campuslink.project.result.Result;
import com.snow.campuslink.user.entity.User;
import com.snow.campuslink.user.service.IUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Gaosl
 * @since 2021-04-02
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseRestController<IUserService, User> {
    @Resource
    IUserService userService;

    @RequestLog()
    @ApiOperation("登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "email", value = "用户邮箱",dataType = "String"),
            @ApiImplicitParam(name = "password", value = "用户密码",dataType = "String")
    })
    @PostMapping("/login")
    public Result Login(String email, String password) {
        QueryWrapper<User> cczuForumUserQueryWrapper = new QueryWrapper<>();
        cczuForumUserQueryWrapper.eq("email",email);
        User one = userService.getOne(cczuForumUserQueryWrapper);
        if (one==null) {
            return Result.partFail().message("用户邮箱不存在，请重新输入");
        }
        if (!one.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))) {
            return Result.partFail().message("用户密码错误，请重新输入");
        }
        return Result.success().data(one);

    }

}

